//
//  JokeTableViewCell.m
//  miaopai
//
//  Created by 杜昕宇 on 15/11/20.
//  Copyright © 2015年 Crow Zhang. All rights reserved.
//

#import "JokeTableViewCell.h"
#import "CoreData.h"
#import "MacroDefinition.h"
#import "UMSocial.h"

@interface JokeTableViewCell()

@property (nonatomic, retain) CoreData *coreDataManager; // 数据库管理

@property (nonatomic, retain) UILabel *timeLabel; // 时间

@property (nonatomic, retain) UILabel *detailLabel; // 详情

@property (nonatomic, retain) UIButton *collectionImage; // 收藏

@property (nonatomic, retain) UIButton *shareImage; // 分享

@property (nonatomic, retain) UIView *lineView; // 一根线

@property (nonatomic, copy) Block myBlock;

@property (nonatomic, copy) shareBlock shareBlock;
@end

@implementation JokeTableViewCell

- (void)dealloc
{
    Block_release(_myBlock);
    [_myJoke release];
    [_coreDataManager release];
    [_timeLabel release];
    [_detailLabel release];
    [_collectionImage release];
    [_shareImage release];
    [_lineView release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.coreDataManager = [CoreData shareManagerWithFile:@"joke"]; // 初始化

        [self creatViews]; // 创建视图
    }
    return self;
}

#pragma mark - 创建视图
- (void)creatViews{
    /************************************  创建 time  ****************************/
    self.timeLabel = [[UILabel alloc] init];
    //    _timeLabel.backgroundColor = [UIColor cyanColor];
    _timeLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_timeLabel];
    [_timeLabel release];
    /************************************ 创建一根线 ****************************/
    self.lineView = [[UIView alloc] init];
    _lineView.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_lineView];
    [_lineView release];

    /************************************  创建 detail  ****************************/
    self.detailLabel = [[UILabel alloc] init];
    //    _detailLabel.backgroundColor = [UIColor cyanColor];
    _detailLabel.numberOfLines = 0;
    [self.contentView addSubview:_detailLabel];
    [_detailLabel release];
    /************************************  创建 收藏  ****************************/
    self.collectionImage = [[UIButton alloc] init];
    //    _collectionImage.backgroundColor = [UIColor cyanColor];
    [_collectionImage addTarget:self action:@selector(classButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.collectionImage setImage:[UIImage imageNamed:@"liked"] forState:UIControlStateNormal];
    [self.contentView addSubview:_collectionImage];
    [_collectionImage release];
    /************************************  创建 分享  ****************************/
    self.shareImage = [[UIButton alloc] init];
    //    _shareImage.backgroundColor = [UIColor cyanColor];
    [_shareImage addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.shareImage setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [self.contentView addSubview:_shareImage];
    [_shareImage release];
}

- (void)shareButtonClicked:(shareBlock)block{
    self.shareBlock = block;
}

- (void)shareAction:(UIButton *)button{
    self.shareBlock();
}

- (void)classButtonClicked:(Block)block{
    self.myBlock = block;
}

#pragma mark - 取消收藏
- (void)classButton:(UIButton *)button {
    self.myBlock();
}

#pragma mark - 高度 text
+ (CGFloat)heightOfText:(NSString *)text{ // 高度text

    //    屏幕宽-30 (20 是 item 距离屏幕距离, 10 是 label 离左边的距离)
    CGSize size = CGSizeMake(SCREEN_WIDTH, MAXFLOAT);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    CGRect label = [text boundingRectWithSize: size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return label.size.height;
}

- (void)setMyJoke:(CollectionJoke *)myJoke {
    if (_myJoke != myJoke) {
        [_myJoke release];
        _myJoke = [myJoke retain];
        self.timeLabel.text = myJoke.created_at;
        self.detailLabel.text = myJoke.text;
    }
}

#pragma mark - 自适应高度
- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat vWidth = SCREEN_WIDTH;
    CGFloat vHeight = SCREEN_HEIGHT / 20;
    CGFloat imageHeight = SCREEN_HEIGHT / 30;
    self.timeLabel.frame = CGRectMake(10 , 0, vWidth - 10, vHeight);
    self.lineView.frame = CGRectMake(0, _timeLabel.Height + _timeLabel.Y , vWidth, 1);
#warning 自适应高度
    self.detailLabel.frame = CGRectMake(_timeLabel.X, _timeLabel.Height + _timeLabel.Y + 5, vWidth - 10, [[self class] heightOfText:_myJoke.text]);
    self.collectionImage.frame = CGRectMake(_detailLabel.X, _detailLabel.Y + _detailLabel.Height + 5, imageHeight , imageHeight);
    self.shareImage.frame = CGRectMake(vWidth - vHeight, _collectionImage.Y, imageHeight, imageHeight);
}

+ (JokeTableViewCell *)JokeTableView:(UITableView *)tableView{ // 类方法
    static NSString *cellIdentify = @"jokeTableViewCell";
    JokeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[JokeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    return cell;

}
@end

